// pages/songs-more/index.js
import {rankingStore,playerStore} from '../../store/index'
import {getSongDetail,getSongDetailList} from '../../service/api_music'
Page({

  data: {
     menuId:0,
     ranktype:"",
    rankingSongs:[],
    menuInfo:{}
    
  },

  onLoad(options) {
    const ranktype=options.type
    this.setData({ranktype})
    if (ranktype==='menu') {
      const id=options.menuId
      this.getDetailSongs(id,0)
      this.setData({menuId:id})
    }else if (ranktype==='rank') {
      rankingStore.onState('newRanking',this.getRankingDataHandle)
    }
  },
  //点击单项歌曲，传入歌曲列表与当前歌曲索引
  handleSongItemClick:function(event) {
    const index = event.currentTarget.dataset.index
    playerStore.setState("songPlayList", this.data.rankingSongs)
    playerStore.setState('songPlayListIndex', index)
  },

  getRankingDataHandle:function(res){
    this.setData({rankingSongs:res})
  },

  getDetailSongs:function(id,offset){
    //可获取全部歌曲，作懒加载
    // const limit=20
    // getSongDetailList(id,limit,offset).then(res=>{
    //   console.log(res);
    //   const songs=res.data.songs
    //   this.setData({rankingSongs:this.data.rankingSongs.concat(songs)})
    // })
    getSongDetail(id).then(res=>{
      console.log(res);
      const info=res.data.playlist
     this.setData({menuInfo:info,rankingSongs:info.tracks})
    })
  },

  onUnload() {
   
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  async onReachBottom() {
    // 懒加载事件
      // if (this.data.ranktype==='menu') {
      // const offset=this.data.rankingSongs.length
      //   this.getDetailSongs(this.data.menuId,offset)
      // }
  },
})